//
//  CroperView.m
//  WULIHH
//
//  Created by chen tang on 16/4/17.
//  Copyright © 2016年 tang. All rights reserved.
//

#import "CroperView.h"
#import "Header.h"


@interface CroperView ()<UIScrollViewDelegate>
{
    UIScrollView *mainScrollView;
    CGSize _imageSize;
    
}


@property (nonatomic, strong) UIImageView *imageView;

@end


@implementation CroperView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self initUI];
    }
    return self;
}


-(void)initUI
{
    mainScrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    [mainScrollView setDelegate:self];
    [mainScrollView setBounces:NO];
    [mainScrollView setBackgroundColor:uicolor(215, 215, 215)];
    [self addSubview:mainScrollView];
}




- (void)layoutSubviews
{
    [super layoutSubviews];
    
    // center the zoom view as it becomes smaller than the size of the screen
    CGSize boundsSize = self.bounds.size;
    CGRect frameToCenter = self.imageView.frame;
    
    // center horizontally
    if (frameToCenter.size.width < boundsSize.width)
        frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
    else
        frameToCenter.origin.x = 0;
    
    // center vertically
    if (frameToCenter.size.height < boundsSize.height)
        frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
    else
        frameToCenter.origin.y = 0;
    
    self.imageView.frame = frameToCenter;
}


- (UIImage *)capture
{
    UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, [UIScreen mainScreen].scale);
    
    [self drawViewHierarchyInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) afterScreenUpdates:YES];
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}



- (void)displayImage:(UIImage *)image
{
    // clear the previous image
    [self.imageView removeFromSuperview];
    self.imageView = nil;
    
    // reset our zoomScale to 1.0 before doing any further calculations
    mainScrollView.zoomScale = 1.0;
    
    // make a new UIImageView for the new image
    self.imageView = [[UIImageView alloc] initWithImage:image];
    self.imageView.clipsToBounds = NO;
    [mainScrollView addSubview:self.imageView];
    
    CGRect frame = self.imageView.frame;
    if (image.size.height > image.size.width) {
        frame.size.width = self.bounds.size.width;
        frame.size.height = (self.bounds.size.width / image.size.width) * image.size.height;
    } else {
        frame.size.height = self.bounds.size.height;
        frame.size.width = (self.bounds.size.height / image.size.height) * image.size.width;
    }
    self.imageView.frame = frame;
    [self configureForImageSize:self.imageView.bounds.size];
}



- (void)configureForImageSize:(CGSize)imageSize
{
    _imageSize = imageSize;
    mainScrollView.contentSize = imageSize;
    
    //to center
    if (imageSize.width > imageSize.height) {
        mainScrollView.contentOffset = CGPointMake(imageSize.width/4, 0);
    } else if (imageSize.width < imageSize.height) {
        mainScrollView.contentOffset = CGPointMake(0, imageSize.height/4);
    }
    
    [self setMaxMinZoomScalesForCurrentBounds];
    mainScrollView.zoomScale = mainScrollView.minimumZoomScale;
}

- (void)setMaxMinZoomScalesForCurrentBounds
{
    mainScrollView.minimumZoomScale = 1.0;
    mainScrollView.maximumZoomScale = 2.0;
}


- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.imageView;
}


- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
//    NSLog(@"%f", scrollView.zoomScale*scrollView.bounds.size.width);
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
